<template>
  <div>
    <Card style="margin-bottom: 10px">
      <LineMulti :chartData="lineMultiData" height="30vh" type="line" :option="lineOptions" />
    </Card>
    <BasicTable @register="registerTable">
      <template #account="{ record }">
        <Tooltip placement="top" :title="record.userName">
          <span> {{ record.userName }} </span>
        </Tooltip>
      </template>
      <template #action="{ record }">
        <TableAction :actions="getActions(record)" />
      </template>
    </BasicTable>
    <FormDetail @register="registerDetail" />
  </div>
</template>
<script lang="ts" name="monitor-login-log" setup>
  import { ref, onMounted } from "vue"
  import { Card, Tooltip } from "ant-design-vue"
  import LineMulti from "/@/components/chart/LineMulti.vue"
  import FormDetail from "./Detail.vue"
  import { BasicTable, TableAction } from "/@/components/Table"
  import { useListPage } from "/@/hooks/system/useListPage"
  import { pageLoginLog } from "/@/api/iam/loginLog"
  import { queryStatistic } from "/@/api/index"
  import { columns, searchFormSchema } from "./data"
  import { useMessage } from "/@/hooks/web/useMessage"
  import { useDrawer } from "/@/components/Drawer"
  import { Icon } from "/@/components/Icon"

  const [registerDetail, { openDrawer: openDetail }] = useDrawer()

  const { createMessage } = useMessage()
  const { prefixCls, tableContext } = useListPage({
    designScope: "quartz-template",
    tableProps: {
      title: "登录日志",
      api: pageLoginLog,
      columns: columns,
      canResize: false,
      actionColumn: {
        width: 110,
        title: "操作",
        dataIndex: "action",
        fixed: false,
        slots: { customRender: "action" },
      },
      formConfig: {
        labelWidth: 120,
        schemas: searchFormSchema,
        fieldMapToTime: [["loginTime", ["startTime", "endTime"], "YYYY-MM-DD HH:mm:ss"]],
      },
    },
  })

  const [registerTable, { reload }] = tableContext

  const lineMultiData = ref<any[]>([])

  const lineOptions = ref({
    title: { text: "周统计", textStyle: { fontWeight: "lighter" } },
    tooltip: {
      trigger: "axis",
      axisPointer: {
        lineStyle: {
          width: 1,
          color: "#019680",
        },
      },
    },
  })

  onMounted(async () => {
    lineMultiData.value = await queryStatistic({ days: 7, biz: "monitor", method: "login" })
  })

  function getActions(record) {
    return [
      {
        label: "详情",
        onClick: handleDetail.bind(null, record),
      }
    ]
  }

  const handleDetail = (record) => openDetail(true, { record })
</script>
